﻿/*
  Project:       DataAnalyse
  File:          QQRelationHandler.ashx
  Description:   
 *               The global verialbles are all in this file.
  Author:        Lanny(damon_lan@163.com)
  Date:          Sept,2014
  version:       2.0
  Modify:
  
  
  
  References:

 
                 
 Comments:
 * Reconstruct the QQRelation function. Merge 4 old files into 1 file.
 * Add log function.


Notes:

*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DataAnalyseTool.Model;
using System.Text;
using System.Data;
using DataAnalyseTool.Utilities;
using DataAnalyseTool.BusinessTier.Bll;

namespace DataAnalyseTool.BusinessTier.Handlers.QQRelateHandler
{
  /// <summary>
  /// Summary description for QQRelateHandler
  /// </summary>
  public class QQRelateHandler : IHttpHandler
  {
    protected static readonly log4net.ILog log =
    log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);

    QQRelationBLL qqrelateBll = new QQRelationBLL();
    QQRelation qqrelationModel = new QQRelation();
    public void ProcessRequest(HttpContext context)
    {

      context.Response.ContentType = "text/plain";
      string type = context.Request.Params["type"];
      switch (type)
      {
        case "Add":
          AddQQRelation(context);
          break;
        case "Delete":
          DeleteQQRelation(context);
          break;
        case "Update":
          UpdateQQRelation(context);
          break;
        case "Get":
          GetQQRelation(context);
          break;
        default:
          log.Error("Get the Type value occur exception, the Type value is: "+type);
          break;

      }
    }

    private void AddQQRelation(HttpContext ctx)
    {
      try
      {
        qqrelationModel.MainQQ = ctx.Request["MainQQ"].ToString();
        qqrelationModel.QQ = ctx.Request["QQ"].ToString();

        var dtExistQQ = qqrelateBll.IsSameQQ(qqrelationModel.MainQQ, qqrelationModel.QQ);
        if (dtExistQQ.Rows.Count == 0)
        {
          qqrelationModel.CreateTime = DateTime.Now.ToString();
          qqrelateBll.AddQQRelation(qqrelationModel);
          string returnValue = "{MainQQ:'" + qqrelationModel.MainQQ + "',QQ:'" + qqrelationModel.QQ + "',CreateTime:'" + qqrelationModel.CreateTime + "'}";
          ctx.Response.Write(returnValue);
        }
        else
        {
          ctx.Response.Write("{Repeater:'1'}");
        }
      }
      catch(Exception ex)
      {
        log.Error("QQ:"+qqrelationModel.QQ+",MainQQ: "+qqrelationModel.MainQQ+",new add QQ Relations occure the exception information: "+ex.Message);
      }
        
     
    }

    private void DeleteQQRelation(HttpContext ctx)
    {
      string id = ctx.Request.Params["id"].ToString();

      if (qqrelateBll.DeleteQQRelation(id) == 1)
      {
        ctx.Response.Write("{'success':true}");
        return;
      }

      ctx.Response.Write("{'success':'false'}");
    }

    private void UpdateQQRelation(HttpContext ctx)
    {
      qqrelationModel.MainQQ = ctx.Request["MainQQ"];
      qqrelationModel.QQ = ctx.Request["QQ"];
      qqrelationModel.QQMappID = Convert.ToInt32(ctx.Request.QueryString["id"]);
      DateTime result;
      bool isOK = DateTime.TryParse(ctx.Request["CreateTime"], out result);
      if (isOK)
      {
        DateTime dt = Convert.ToDateTime(ctx.Request["CreateTime"]);
        qqrelationModel.CreateTime = dt.ToString();
      }
      else
      {
        qqrelationModel.CreateTime = DateTime.Now.ToString();
      }
      qqrelateBll.UpdateQQRelation(qqrelationModel);
      string returnValue = "{MainQQ:'" + qqrelationModel.MainQQ + "',QQ:'" + qqrelationModel.QQ + "',CreateTime:'" + qqrelationModel.CreateTime + "'}";
      ctx.Response.Write(returnValue);
    }

    //Get the all QQ Numbers.
    private void GetQQRelation(HttpContext ctx)
    {
      try
      {
        StringBuilder sbRet = new StringBuilder();
        DataTable dt;

        string txtQQ = ctx.Request["txtQQ"];
        if (txtQQ != null && txtQQ.Trim() != "")
        {
          dt = qqrelateBll.getSameQQ(txtQQ);
          sbRet.Append(Utility.GetJsonString(dt));
          ctx.Response.Write(sbRet);
        }
        else
        {
          string page = ctx.Request["page"];
          string rows = ctx.Request["rows"];
          string sort = ctx.Request["sort"];
          string order = ctx.Request["order"];

          DataView dv = null;
          dt = qqrelateBll.GetAllRelations();
          dv = dt.DefaultView;
          dv.Sort = sort + " " + order;

          dt = dv.ToTable();
          dt.Columns.Add("id");
          for (int i = 0; i < dt.Rows.Count; i++)
          {
            dt.Rows[i]["id"] = dt.Rows[i]["QQMappID"];
          }
          sbRet.Append(Utility.GetJsonString(dt));

          ctx.Response.Write(sbRet);
        }
      }
      catch (Exception ex)
      {

      }
    }

    public bool IsReusable
    {
      get
      {
        return false;
      }
    }
  }
}